package com.yimils.core.sys.dto;



import com.yimils.core.sys.mapper.ResourceMapper;
import com.yimils.core.sys.po.ResourceDo;
import com.yimils.core.sys.vo.ResourceTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Component
public class ResourceDto {

    @Autowired(required = false)
    private ResourceMapper resourceMapper;

    public ResourceTreeVo getResourceTreeVo() {
        ResourceTreeVo vo = new ResourceTreeVo();
        vo.setResources(new ArrayList<>());
        List<ResourceDo> dirs = resourceMapper.getResourceByParentId(null);
        //创建一二级菜单项
        if (dirs != null) {
            for (ResourceDo dir : dirs) {
                vo.getResources().add(makeTreeNode(dir));
            }
        }
        return vo;
    }


    private ResourceTreeVo.ResourceNode makeTreeNode(ResourceDo dir) {
        ResourceTreeVo.ResourceNode node = makeMidNode(dir);
        List<ResourceDo> group = resourceMapper.getResourceGroupByParentId(node.getId());
        if (!CollectionUtils.isEmpty(group)) {
            node.setChildren(new ArrayList<>());
            for (ResourceDo resourceDo : group) {
                node.getChildren().add(makeTreeNode(resourceDo));
            }
        } else {
            List<ResourceDo> endPoints = resourceMapper.getResourceEndPoint(dir.getParentId(), dir.getName());
            if (!CollectionUtils.isEmpty(endPoints)) {
                node.setChildren(new ArrayList<>());
                for (ResourceDo endPoint : endPoints) {
                    node.getChildren().add(makeEndPointNode(endPoint));
                }
            }
        }
        return node;
    }

    //构建不含child的node
    private ResourceTreeVo.ResourceNode makeEndPointNode(ResourceDo dir) {
        ResourceTreeVo.ResourceNode node = new ResourceTreeVo.ResourceNode();
        node.setId(dir.getId());
        String optSuffix = getMenuOptTitle(dir.getOpt());
        if (optSuffix != null) {
            node.setLabel(dir.getTitle() + "-" + optSuffix);
        } else {
            node.setLabel(dir.getTitle());
        }
        node.setParentId(dir.getParentId());
        node.setName(dir.getName());
        node.setIcon(dir.getIcon());
        node.setOpt(dir.getOpt());
        node.setUrl(dir.getUrl());
        node.setSort(dir.getSort());
        node.setSys(dir.getSys());
        return node;
    }

    private ResourceTreeVo.ResourceNode makeMidNode(ResourceDo dir) {
        ResourceTreeVo.ResourceNode node = new ResourceTreeVo.ResourceNode();
        node.setId(dir.getId());
        node.setLabel(dir.getTitle());
        node.setParentId(dir.getParentId());
        node.setName(dir.getName());
        node.setIcon(dir.getIcon());
        node.setOpt(dir.getOpt());
        node.setUrl(dir.getUrl());
        node.setSort(dir.getSort());
        node.setSys(dir.getSys());
        return node;
    }

    private String getMenuOptTitle(String opt) {
        if (opt != null) {
            switch (opt) {
                case "view":
                    return "查看";
                case "add":
                    return "添加";
                case "update":
                    return "修改";
                case "remove":
                    return "删除";
            }
        }
        return null;
    }
}
